using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace ApplicationLibrary.ExtendedControls
{
    [Designer(typeof(MySnapLinesDesigner))]
    public partial class RadarTextBox : UserControl, ISnapLineControl
    {
        public Control textBoxValue { get { return textBox1; } }

        public Control labelValue { get { return null; } }

        protected RadarForm rdrRadarForm = null;

        public RadarTextBox()
        {
            InitializeComponent();
            button1.Enabled = false;
            textBox1.CharacterCasing = CharacterCasing.Upper;
            DelegateValidating = false;
            EnableAddOnFly = true;
        }

        #region Public Properties

        public bool EnableAddOnFly { get; set; }

        public bool DelegateValidating { get; set; }

        public string Description { get; private set; }

        public Control DescriptionControl { private get; set; }

        public override string Text
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public bool TrimResult
        {
            get { return textBox1.TrimResult; }
            set { textBox1.TrimResult = value; }
        }

        public RadarForm RadarForm
        {
            set
            {
                rdrRadarForm = value;
                button1.Enabled = value != null;
                if (rdrRadarForm != null)
                    rdrRadarForm.RadarFormRowSelected += new RadarForm.RadarFormRowSelectedEventHandler(rdrRadarForm_RadarFormRowSelected);
            }
            get
            {
                return rdrRadarForm;
            }
        }

        public int SelectionLength
        {
            set { textBox1.SelectionLength = value; }
            get { return textBox1.SelectionLength; }
        }

        public int SelectionStart
        {
            set { textBox1.SelectionStart = value; }
            get { return textBox1.SelectionStart; }
        }

        public CharacterCasing CharacterCasing
        {
            get { return textBox1.CharacterCasing; }
            set { textBox1.CharacterCasing = value; }
        }

        public bool ReadOnly
        {
            get { return textBox1.ReadOnly; }
            set
            {
                textBox1.ReadOnly = value;
                button1.Enabled = !value;
            }
        }

        #endregion


        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            if (rdrRadarForm == null) return;

            IRadarParameters iParam = rdrRadarForm.GetRadarParameters(Text);
            if (textBox1.Text != "" && !rdrRadarForm.Find(iParam))
            {
                if (EnableAddOnFly && rdrRadarForm.CanOpenNew)
                {
                    DialogResult result = MessageBox.Show(Properties.Resources.Msg_InsertNewCode,
                                                            Properties.Resources.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        rdrRadarForm.OpenNew(iParam);
                        textBox1.Focus();
                    }
                }
                else
                    MessageBox.Show(Properties.Resources.Msg_CodeNotFound,
                                                            Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
                textBox1.Focus();
            }
            else
            {
                if (textBox1.Text == string.Empty)
                {
                    Description = string.Empty;
                    return;
                }
            }
            Description = rdrRadarForm.Description;
        }

        private void rdrRadarForm_RadarFormRowSelected(object sender, RadarFormRowSelectedArgs pe)
        {
            textBox1.Text = RadarForm.GetCodeFromParameters(pe.parameters);
            if (DescriptionControl != null)
                DescriptionControl.Text = rdrRadarForm.Description;

            Description = rdrRadarForm.Description;
            OnValidating(new CancelEventArgs());
        }

        public void UpdateDescription()
        {
            IRadarParameters iParam = rdrRadarForm.GetRadarParameters(Text);
            DescriptionControl.Text = (Text != string.Empty && rdrRadarForm.Find(iParam))
                                    ? rdrRadarForm.Description
                                    : "";
        }

        private void RadarTextBox_Paint(object sender, PaintEventArgs e)
        {
            button1.Width = button1.Height + 1;
            button1.Height = button1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;

            //button1.Font = new Font("Webdings", this.Font.GetHeight() - this.Font.GetHeight() / 4);
            this.Height = textBox1.Height;
        }

        private void RadarTextBox_Resize(object sender, EventArgs e)
        {
            button1.Width = button1.Height + 1;
            button1.Height = textBox1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;

            // button1.Font = new Font("Webdings", this.Font.GetHeight() - this.Font.GetHeight() / 4);
            this.Height = textBox1.Height;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (DescriptionControl != null && !textBox1.Focused)
                UpdateDescription();

            OnTextChanged(e);
        }

        private void textBox1_LostFocus(object sender, System.EventArgs e)
        {
            OnLostFocus(e);
        }

        private void textBox1_GotFocus(object sender, System.EventArgs e)
        {
            OnGotFocus(e);
        }

        private void RadarTextBox_Validating(object sender, CancelEventArgs e)
        {

        }

        private void RadarTextBox_Validated(object sender, EventArgs e)
        {
            if (rdrRadarForm == null || DescriptionControl == null) return;

            DescriptionControl.Text = rdrRadarForm.Description;
            Description = rdrRadarForm.Description;
        }

        private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyData == Keys.F8)
            {
                Search();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            if (rdrRadarForm != null)
            {
                rdrRadarForm.Seed = textBox1.Text;
                if (rdrRadarForm.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Focus();
                }
            }
        }
    }
}